﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.BLL;
using KPIS.GERP.GBMS.MODEL;
//using KPIS.Utils;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;

namespace KPIS.GERP.GBMS.APP_UI
{
    public partial class BGM_Project_Approve : System.Web.UI.Page
    {
        private ProjectApprove_BLL projectBLL = new ProjectApprove_BLL();
        private EmployeeBLL empBLL = new EmployeeBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            CreateNewSequence(Convert.ToInt16(lbCountRow.Text), "");
            if (!IsPostBack)
            {
                loadDataGrid_t1();
            }
        }
        protected void rdoModeRD_t1_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadDataGrid_t1();
        }


        private void loadDataGrid_t1()
        {

            Grid_t1.DataSource = projectBLL.GetDataProjectByYear(2553);

            if (rdoModeRD_t1.Items[0].Selected)
            {
                Grid_t1.MasterTableView.GroupByExpressions.Clear();
                GridGroupByExpression g = new GridGroupByExpression(Grid_t1.Columns[2]);
                Grid_t1.MasterTableView.GroupByExpressions.Add(g);
                Grid_t1.Columns[2].Visible = false;
                Grid_t1.Columns[3].Visible = true;
            }
            else
            {
                Grid_t1.MasterTableView.GroupByExpressions.Clear();
                GridGroupByExpression g = new GridGroupByExpression(Grid_t1.Columns[3]);
                Grid_t1.MasterTableView.GroupByExpressions.Add(g);
                Grid_t1.Columns[2].Visible = true;
                Grid_t1.Columns[3].Visible = false;
            }
            Grid_t1.DataBind();
            //if (type != "ap")
            //{
            //    Grid_t1.Columns[5].Visible = false;
            //}
        }

        # region Create New Sequence
        private void CreateNewSequence(int nCount, string mode)
        {
            if (nCount > 0)
            {

                for (int i = 0; i < nCount; i++)
                {
                    TableRow tr = new TableRow();

                    TableCell tc1 = new TableCell();
                    TableCell tc2 = new TableCell();
                    TableCell tc3 = new TableCell();
                    TableCell tc4 = new TableCell();

                    RadComboBox cb1 = new RadComboBox();
                    RadComboBox cb2 = new RadComboBox();

                    ImageButton del = new ImageButton();

                    TextBox txt = new TextBox();

                    // ------ Set TextBox Property --------------

                    txt.TextMode = TextBoxMode.MultiLine;
                    txt.Width = 300;
                    txt.Height = 100;

                    // ------ Set ImageButton Property ----------

                    del.ImageUrl = "../Image/Grid/Cancel.gif";
                    del.Click += new ImageClickEventHandler(this.btnDel_Click);

                    // ------ Add VALUES and Text To ComboBox ----

                    for (int j = 0; j < tb.Rows.Count; j++)
                    {
                        cb1.Items.Add(new RadComboBoxItem((j + 1).ToString(), (j + 1).ToString()));
                    }

                    // ------ Set ComboBox Properties -----

                    cb1.Width = 50;
                    cb2.Width = 250;

                    cb1.ID = "cbSeq" + (tb.Rows.Count).ToString();
                    cb2.ID = "cbExecPos" + (tb.Rows.Count).ToString();
                    txt.ID = "txtComment" + (tb.Rows.Count).ToString();
                    DataTable infoList = empBLL.ListEmployeePosByType("1");
                    cb2.DataTextField = "full_name";
                    cb2.DataValueField = "sequence";
                    cb2.DataSource = infoList;
                    cb2.DataBind();


                    cb1.SelectedIndex = cb1.Items.Count - 1;

                    // ------ Set Cells Style ------

                    tc1.Height = 120;
                    tc2.Height = 120;
                    tc4.Height = 120;
                    tc2.HorizontalAlign = HorizontalAlign.Left;
                    tc1.VerticalAlign = VerticalAlign.Top;
                    tc2.VerticalAlign = VerticalAlign.Top;
                    tc3.VerticalAlign = VerticalAlign.Top;
                    tc4.VerticalAlign = VerticalAlign.Top;

                    // ------ Add ComboBox to Cells -----

                    tc1.Controls.Add(cb1);
                    tc2.Controls.Add(cb2);
                    tc3.Controls.Add(txt);
                    tc4.Controls.Add(del);

                    // ------ Add Cells To Rows

                    tr.Cells.AddAt(0, tc1);
                    tr.Cells.AddAt(1, tc2);
                    tr.Cells.AddAt(2, tc3);
                    tr.Cells.AddAt(3, tc4);

                    // ------ Add Row To Table

                    tb.Rows.Add(tr);
                }
            }
            else
                lbCountRow.Text = "0";
        }
        # endregion

        protected void Grid_t1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                if (e.Item.Cells[9].Text == "A")
                {
                    e.Item.Cells[3].Text = "";
                }
            }
        }
        protected void Grid_t1_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.ExpandCollapseCommandName)
            {
                string key_grid1 = (e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Sequence"].ToString();
                DataTable dt = projectBLL.GetDataProjectBySequenceForGridIn(key_grid1);

                RadGrid grdIn_t1 = (RadGrid)((GridDataItem)e.Item).ChildItem.FindControl("GridIn_t1");
                grdIn_t1.DataSource = dt;
                grdIn_t1.DataBind();
                //if (type != "ap")
                //{
                //    grdIn_t1.Columns[1].Visible = false;
                //}
                return;
            }
            else
            { }
            loadDataGrid_t1();
        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            lbCountRow.Text = (Convert.ToInt16(lbCountRow.Text) + 1).ToString();
            CreateNewSequence(1, "add");
        }

        protected void btnDel_Click(object sender, ImageClickEventArgs e)
        {
            if (Convert.ToInt16(lbCountRow.Text) > 0)
            {
                
                string z = e.ToString();
                tb.Rows.RemoveAt(tb.Rows.Count - 1);
                lbCountRow.Text = (Convert.ToInt16(lbCountRow.Text) - 1).ToString();
            }
            else
            {
                lbCountRow.Text = "0";
            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            int faq = 0;
            foreach (GridDataItem data in Grid_t1.MasterTableView.GetSelectedItems())
            {
                faq = 1;
                string key = data.OwnerTableView.DataKeyValues[data.ItemIndex]["Sequence"].ToString();
                for (int i = 1; i <= Convert.ToInt16(lbCountRow.Text); i++)
                {
                    RadComboBox cb1 = (RadComboBox)tb.Rows[i - 1].FindControl("cbSeq" + i.ToString());
                    RadComboBox cb2 = (RadComboBox)tb.Rows[i - 1].FindControl("cbExecPos" + i.ToString());
                    TextBox txt = (TextBox)tb.Rows[i - 1].FindControl("txtComment" + i.ToString());
                    string[] emp = cb2.SelectedValue.Split('-');
                    projectBLL.InsertProjectStatus(key, cb1.SelectedValue, emp[0], emp[1], txt.Text);
                }
            }
            if (faq != 0)
            {
                clear();
                loadDataGrid_t1();
                RadManager1.Alert("บันทึกเรียบร้อยแล้ว");
            }
            else 
            {
                loadDataGrid_t1();
                RadManager1.Alert("กรุณาเลือก งาน/โครงการ !");
            }
            
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            clear();
        }
        private void clear()
        {
            for (int i = 1; i <= Convert.ToInt16(lbCountRow.Text); i++)
            {
                RadComboBox cb1 = (RadComboBox)tb.Rows[i - 1].FindControl("cbSeq" + i.ToString());
                RadComboBox cb2 = (RadComboBox)tb.Rows[i - 1].FindControl("cbExecPos" + i.ToString());
                TextBox txt = (TextBox)tb.Rows[i - 1].FindControl("txtComment" + i.ToString());
                cb1.SelectedIndex = 0;
                cb2.SelectedIndex = 0;
                txt.Text = "";
            }
        }

    }
}
